package tech.aistar.corejava.day17.anno;

import java.lang.reflect.Method;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:反射获取注解的信息
 * @date 2019/8/7 0007
 */
public class ReflectAnnoTest {
    public static void main(String[] args) {
        Class<?> c = TestMyAnno2.class;

        //判断TestMyAnno2这个类上面是否加了注解
        boolean flag = c.isAnnotationPresent(MyAnno2.class);
        System.out.println(flag);

        if(flag){
            //获取注解的属性的值

            //1 - 获取注解的实例 - 反射
            MyAnno2 ma = c.getAnnotation(MyAnno2.class);

            String result = ma.value();

            System.out.println("result:"+result);

        }

        System.out.println("===============");

        //拿到test方法对应的Method实例
        try {
            Method m = c.getDeclaredMethod("Test");

            //判断方法上面是否添加了注解
            System.out.println(m.isAnnotationPresent(MyAnno2.class));

            if(m.isAnnotationPresent(MyAnno2.class)){
                MyAnno2 m2 = m.getDeclaredAnnotation(MyAnno2.class);

                String sql = m2.value();

                System.out.println(sql);
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
